package com.soho.helper;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.soho.core.Mt4;
import com.soho.model.Symbols;
import com.soho.model._MappingKit;

import online.mtapi.mt4.Bar;
import online.mtapi.mt4.QuoteHistoryEventArgs;
import online.mtapi.mt4.QuoteHistoryEventHandler;
import online.mtapi.mt4.Timeframe;

/**
 * 初始化数据
 * 
 * @author hubs
 */
public class SymbolInit {
	private static ExecutorService executor = Executors.newCachedThreadPool();

	public static void main(String[] args) {
		initSymbol();
	}

	private static void initDb() {
		Prop p = PropKit.useFirstFound("demo-config-pro.txt", "demo-config-dev.txt");
		DruidPlugin dp = new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password"));
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		// 所有映射在 MappingKit 中自动化搞定
		_MappingKit.mapping(arp);
		dp.start();
		arp.start();
	}

	private static Kv getSymbolDbList(String symbole) {
		Kv record = Kv.create();
		long defaultLong =LocalDateTime.now(ZoneId.of(ContentHelper.ZONE_DEFAULT)).plusMinutes(10).toEpochSecond(ZoneOffset.UTC);
		long time_m1 = SqlHelper.getLastTime(symbole, ContentHelper.TABLE_QUOTES_M1, defaultLong);
		long time_m5 = SqlHelper.getLastTime(symbole, ContentHelper.TABLE_QUOTES_M5, defaultLong);
		long time_m15 = SqlHelper.getLastTime(symbole, ContentHelper.TABLE_QUOTES_M15, defaultLong);
		long time_m30 = SqlHelper.getLastTime(symbole, ContentHelper.TABLE_QUOTES_M30, defaultLong);
		long time_h1 = SqlHelper.getLastTime(symbole, ContentHelper.TABLE_QUOTES_H1, defaultLong);
		long time_h4 = SqlHelper.getLastTime(symbole, ContentHelper.TABLE_QUOTES_H4, defaultLong);
		long time_d1 = SqlHelper.getLastTime(symbole, ContentHelper.TABLE_QUOTES_D1, defaultLong);
		long time_w = SqlHelper.getLastTime(symbole, ContentHelper.TABLE_QUOTES_W, defaultLong);
		long time_mn = SqlHelper.getLastTime(symbole, ContentHelper.TABLE_QUOTES_MN, defaultLong);

		record
		//.set(ContentHelper.TABLE_QUOTES_M1, Kv.by("time", time_m1).set("frame", Timeframe.M1))
		//.set(ContentHelper.TABLE_QUOTES_M5,  Kv.by("time", time_m5).set("frame", Timeframe.M5))
		//.set(ContentHelper.TABLE_QUOTES_M15,  Kv.by("time", time_m15).set("frame", Timeframe.M15))
		//.set(ContentHelper.TABLE_QUOTES_M30,  Kv.by("time", time_m30).set("frame", Timeframe.M30))
		//.set(ContentHelper.TABLE_QUOTES_H1,  Kv.by("time", time_h1).set("frame", Timeframe.H1))
		//.set(ContentHelper.TABLE_QUOTES_H4,  Kv.by("time", time_h4).set("frame", Timeframe.H4))
		//.set(ContentHelper.TABLE_QUOTES_D1,  Kv.by("time", time_d1).set("frame", Timeframe.D1))
		.set(ContentHelper.TABLE_QUOTES_W,  Kv.by("time", time_w).set("frame", Timeframe.W1));
		//		.set(ContentHelper.TABLE_QUOTES_MN, Kv.by("time", time_mn).set("frame", Timeframe.MN1));

//		record.set(ContentHelper.TABLE_QUOTES_MN, Kv.by("time", time_mn).set("frame", Timeframe.MN1))
//		record.set(ContentHelper.TABLE_QUOTES_D1,  Kv.by("time", time_d1).set("frame", Timeframe.D1))
//		.set(ContentHelper.TABLE_QUOTES_W,  Kv.by("time", time_w).set("frame", Timeframe.W1))
		;
		return record;
	}

	@SuppressWarnings("unchecked")
	public static void initSymbol() {
		initDb();

		Mt4 me = Mt4.getInstance();
		// 查询目前激活的数据
		List<Symbols> lists = Symbols.getList();
		for (Symbols row : lists) {
			// 货币
			String symbol = row.getSymbol();
			Kv items = getSymbolDbList(symbol);
			items.forEach((table, kv) -> {
				String tableName = table.toString();
				Kv kvItem = (Kv) kv;
				String time = kvItem.getStr("time");
				Timeframe timeframe = (Timeframe) kvItem.get("frame");
				LocalDateTime fromTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(Long.valueOf(time.toString())), ZoneId.of(ContentHelper.ZONE_DEFAULT));
				//LocalDateTime fromTime=LocalDateTime.now(ZoneId.of(ContentHelper.ZONE_DEFAULT));
				PrintHelper.print("INIT Symbol = " + symbol + " From =" + fromTime+ "Table = "+tableName);
				// qc.DownloadQuoteHistory("EURUSD", Timeframe.H1, LocalDateTime.now(), (short)100);
				me.saveHistoryQuotes(tableName,symbol,timeframe, fromTime,1000);
			});
		}
	}
}
